/**
 * Created with IntelliJ IDEA.
 * User: snowfigure
 * Date: 15-4-3
 * Time: 下午9:39
 * To change this template use File | Settings | File Templates.
 */





$(function () {


    $('#tabs').tabs({
        border: false
    });


    $('#article_dg').datagrid({
        url: '/cms/article/list',
        columns: [[
            {field: 'id', title: '文章编号', width: $(this).width() * 0.05, halign: 'center', align: 'center',hidden: true},
            {field: 'type_name', title: '分类', width: $(this).width() * 0.13, halign: 'center', align: 'left'},
            {field: 'title', title: '文章名称', width: $(this).width() * 0.6, halign: 'center', align: 'left'},
            {field: 'create_time', title: '创建时间', width: $(this).width() * 0.08, halign: 'center', align: 'center'},
            {field: 'update_time', title: '更新时间', width: $(this).width() * 0.08, halign: 'center', align: 'center'}
        ]],
        singleSelect: true,//是否单选
        rownumbers: true,//行号
        toolbar: [ {
            text: "删除",
            iconCls: 'icon-remove',
            handler: function () {
                var row = $('#article_dg').datagrid("getSelected");
                if (row == null) {
                    $.messager.alert('错误', '请选择一条记录！', 'info');
                    return;
                }
                $.messager.confirm('确认删除', '确认删除名称为[' + row.name + "]的记录", function (r) {
                    if (r) {
                        $.ajax({
                            type: 'POST',
                            url: '/cms/article/del/' + row.id,
                            success: function (data) {
                                if (data == "true") {
                                    $.messager.alert('成功', '删除成功！', 'info');
                                    $("#nav_dg").datagrid("reload");
                                } else {
                                    $.messager.alert('错误', '出现未知的错误<br/>操作失败！', 'info');
                                }
                            }
                        });//$.ajax
                    }
                });//$.messager.confirm

            }//handler: function()
        },'-',{
            /** 点击新增按钮 */
            text: "新增",
            iconCls: 'icon-add',
            handler: function () {
                detail("/cms/article/add/", "新增");
            }

        },{
            /** 点击新增按钮 */
            text: "编辑",
            iconCls: 'icon-edit',
            handler: function () {
                var row = $('#article_dg').datagrid("getSelected");
                if (row == null) {
                    $.messager.alert('错误', '请先选择一篇文章！', 'info');
                    return;
                }
                articleEdit( row);
            }

        }],
        pagination: true //分页控件
    });
//设置分页控件
    var pt = $('#article_dg').datagrid('getPager');
    $(pt).pagination({
        pageSize: 10,//每页显示的记录条数，默认为10
        pageList: [10, 20, 50, 100],//可以设置每页记录条数的列表
        beforePageText: '第',//页数文本框前显示的汉字
        afterPageText: '页    共 {pages} 页',
        displayMsg: '当前显示 {from} - {to} 条记录   共 {total} 条记录'
    });


    function articleEdit(row) {
        var articleTitleShrot = "";
        console.log("/cms/article/articleIndex/" + row.id);
        var url = "/cms/article/articleIndex/" + row.id;
        if (row.title) {
            articleTitleShrot = row.title.substr(0, 10);
        }

        if ($('#tabs').tabs("exists", articleTitleShrot)) {
            $('#tabs').tabs("select", articleTitleShrot);
        } else {
            var content = '<iframe class="ajaxPageIframe1" scrolling="auto" frameborder="0"  src="' + url + '" style="width:100%;"></iframe>';
            $('#tabs').tabs('add', {
                title: articleTitleShrot,
                content: content,
                closable: true
            });
            var height = $(window).height();
            $(".ajaxPageIframe1").attr("height", height - 25);
        }
    }

});
